using System.Collections.Specialized;
using System.ComponentModel.Design;
using EnvDTE;
using Microsoft.Practices.Common;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;

namespace PrismFactory.SoftwareFactory.Extensions.ValueProviders.VisualStudio
{
    /// <summary>
    /// Search for the project specified by the "Path" attributte in the XML configuration file
    /// </summary>
    /// <example>&lt;ValueProvider Type="ProjectByNameProvider" PathExpression="$(BusinessLayer)\SubProject"/></example>
    [ServiceDependency(typeof (DTE))]
    public class ProjectByPathExpressionProvider : ValueProvider, IAttributesConfigurable
    {
        private string pathExpression;

        #region Overrides

        /// <summary>
        /// Uses <see cref="DteHelper.FindProjectByPath"/> to search for the project specified by the "PathExpression" attributte
        /// </summary>
        /// <param name="currentValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        /// <seealso cref="ValueProvider.OnBeforeActions"/>
        /// <seealso cref="DteHelper.FindProjectByPath"/>
        /// 
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            if (currentValue == null)
            {
                newValue = GetValue();
                if (newValue != null)
                {
                    return true;
                }
            }
            newValue = currentValue;
            return false;
        }

        /// <summary>
        /// Contains code that will be called before actions are executed.
        /// </summary>
        /// <param name="currentValue">An <see cref="T:System.Object"/> that contains the current value of the argument.</param>
        /// <param name="newValue">When this method returns, contains an <see cref="T:System.Object"/> that contains
        /// the new value of the argument, if the returned value
        /// of the method is <see langword="true"/>. Otherwise, it is ignored.</param>
        /// <returns>
        /// 	<see langword="true"/> if the argument value should be replaced with
        /// the value in <paramref name="newValue"/>; otherwise, <see langword="false"/>.
        /// </returns>
        /// <remarks>By default, always returns <see langword="false"/>, unless overridden by a derived class.</remarks>
        public override bool OnBeforeActions(object currentValue, out object newValue)
        {
            if (currentValue == null)
            {
                newValue = GetValue();
                if (newValue != null)
                {
                    return true;
                }
            }
            newValue = currentValue;
            return false;
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <returns></returns>
        private object GetValue()
        {
            var dte = (DTE) GetService(typeof (DTE));
            object newValue = null;

            var evaluator = new ExpressionEvaluationService();
            var dictservice = (IDictionaryService) GetService(typeof (IDictionaryService));

            var path = evaluator.Evaluate(pathExpression, new ServiceAdapterDictionary(dictservice));
            if (path != null)
            {
                newValue = DteHelper.FindProjectByPath(dte.Solution, path.ToString());
            }
            return newValue;
        }

        #endregion

        #region IAttributesConfigurable Members

        /// <summary>
        /// Configures the component using the dictionary of attributes specified
        /// in the configuration file.
        /// </summary>
        /// <param name="attributes">The attributes in the configuration element.</param>
        void IAttributesConfigurable.Configure(StringDictionary attributes)
        {
            pathExpression = attributes["PathExpression"];
        }

        #endregion
    }
}